package com.sky.service.impl;

import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.SetMealDishMapper;
import com.sky.mapper.SetMealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetMealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class SetMealServiceImpl implements SetMealService {

    @Autowired
    private SetMealMapper setMealMapper;
    @Autowired
    private SetMealDishMapper setMealDishMapper;

    /**
     * 分页查询
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageSplit(SetmealPageQueryDTO setmealPageQueryDTO) {

        Long total = setMealMapper.pageCount();
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealPageQueryDTO, setmeal);
        int start = (setmealPageQueryDTO.getPage() - 1) * setmealPageQueryDTO.getPage();

        List<SetmealVO> setmealVOList =
                setMealMapper.pageSplit(start,setmealPageQueryDTO.getPageSize()
                        ,setmeal.getName(),setmeal.getCategoryId(),setmeal.getStatus());

        return new PageResult(total,setmealVOList);
    }

    /**
     * 新增套餐
     * @return null
     */
    @Transactional
    @Override
    public void save(SetmealDTO setmealDTO) {
        // 复杂类有集合，先处理没集合，在处理有集合的最后
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmeal.setStatus(StatusConstant.DISABLE);
        setMealMapper.insertSetMeal(setmeal);
        // 获取刚插入的套餐的id
        long gottenId = setmeal.getId();
        // 处理套餐菜品关系
        List<SetmealDish> setmealDishList = setmealDTO.getSetmealDishes();
        if (setmealDishList != null && setmealDishList.size() != 0){
            for (SetmealDish setmealDish : setmealDishList){
                // 添加套餐id
                setmealDish.setSetmealId(gottenId);
            }
            setMealDishMapper.insertSetMealDish(setmealDishList);
        }
    }

    /**
     * 根据id查询
     * */
    @Override
    public SetmealVO getById(Long id) {
        SetmealVO setmealVO = setMealMapper.getById(id);
        // 设置里面的list集合
        setmealVO.setSetmealDishes(setMealDishMapper.getById(id));
        return setmealVO;
    }

    /**
     *  停售与起售
     * */
    @Override
    public void startOrStop(Integer status, Long id) {
        setMealMapper.startOrStop(status,id);
    }
    /**
     * 修改套餐
     **/
    @Override
    public void update(SetmealDTO setmealDTO) {
        // 1、先修改setmeal
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setMealMapper.update(setmeal);
        // 2、删除原来的setmeal_dish
        Long sid = setmeal.getId();
        /// 删除setmeal_dish中的套餐id值的所有套餐id值
        List<Long> sids = new ArrayList<>();
        sids.add(sid);
        setMealDishMapper.deleteBySetMealId(sids);

        // 3、在修改setmeal_dish,将集合地址传给setmealDishList
        List<SetmealDish> setmealDishList = setmealDTO.getSetmealDishes();
        if (setmealDishList != null && setmealDishList.size() > 0){
            for (SetmealDish setmealDish: setmealDishList){
                // 重新把setmeal的id值赋值给setmealDish
                setmealDish.setSetmealId(sid);
            }
            // 被删了就没了，所以只有再次插入
            setMealDishMapper.insertSetMealDish(setmealDishList);
        }
    }
    /**
     *  删除套餐
     * */
    @Override
    public void deleteGroup(List<Long> ids) {
        // 1、查询套餐状态，判断是否可以删除
        List<SetmealVO> setmealVOList = new ArrayList<>();
        for (Long id : ids){
            setmealVOList.add(getById(id));
        }
        // 2、判断状态是否为启动状态
        for (SetmealVO setmealVO : setmealVOList){
            if (setmealVO.getStatus() == StatusConstant.ENABLE){
                throw new DeletionNotAllowedException("当前状态不能删除");
            }
        }
        setMealMapper.deleteSetMeal(ids);
        setMealDishMapper.deleteBySetMealId(ids);

    }
                                       /*User*/
    /*-------------------------------------------------------------------------------------*/
    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setMealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setMealMapper.getDishItemBySetmealId(id);
    }
}
